#include "mygrid.h"

void lv_mainstart_1(void)
{
    // 3 列宽70像素 3行高50像素   LV_GRID_TEMPLATE_LAST数组结束标志
	static lv_coord_t col_dsc[] = { 70, 70, 70, LV_GRID_TEMPLATE_LAST };
	static lv_coord_t row_dsc[] = { 50, 50, 50, LV_GRID_TEMPLATE_LAST };
	/* 创建一个容器 */
	lv_obj_t* cont = lv_obj_create(lv_scr_act());
	/* 设置网格的列数量 */
	lv_obj_set_style_grid_column_dsc_array(cont, col_dsc, 0);
	/* 设置网格的行数量 */
	lv_obj_set_style_grid_row_dsc_array(cont, row_dsc, 0);
	/* 设置容器大小 */
	lv_obj_set_size(cont, 300, 220);
	lv_obj_center(cont);
	/* 开启 GRID 网格 */
	lv_obj_set_layout(cont, LV_LAYOUT_GRID);

	lv_obj_t* obj;
	/* 创建一个 btn 子类 */
	obj = lv_btn_create(cont);
	/* 把 btn 对象添加到网格(0,0)位置 */
	lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 0,	1,LV_GRID_ALIGN_STRETCH, 0, 3);
	/* 创建一个 switch 子类 */
	obj = lv_switch_create(cont);
	/* 把 btn 对象添加到网格(1,1)位置 */
	lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 1, 2,LV_GRID_ALIGN_STRETCH, 1, 1);
}

void lv_mainstart_2(void)
{
     static lv_coord_t col_dsc[] = {70, 70, 90, LV_GRID_TEMPLATE_LAST};  //列宽度
     static lv_coord_t row_dsc[] = {80, 50, 50, LV_GRID_TEMPLATE_LAST};  //行宽度
 
    /*Create a container with grid*/
    lv_obj_t * cont = lv_obj_create(lv_scr_act());
    lv_obj_set_style_grid_column_dsc_array(cont, col_dsc, 0);
    lv_obj_set_style_grid_row_dsc_array(cont, row_dsc, 0);
    lv_obj_set_size(cont, 500, 500);
    lv_obj_center(cont);
    lv_obj_set_layout(cont, LV_LAYOUT_GRID);

    lv_obj_t * label;
    lv_obj_t * obj;

    uint32_t i;
    for(i = 0; i < 9; i++) {
        uint8_t col = i % 3;
        uint8_t row = i / 3;

        obj = lv_btn_create(cont);
        /*Stretch the cell horizontally and vertically too Set span to 1 to make the cell 1 column/row sized*/
        lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_CENTER, col, 1,  LV_GRID_ALIGN_CENTER, row, 1);

        label = lv_label_create(obj);
        lv_label_set_text_fmt(label, "c%d, r%d", col, row);
        lv_obj_center(label);
    }
}

void mygrid_example(void)
{
    lv_mainstart_2();
}